/*
 * @Author: fk 1031041088@qq.com
 * @Date: 2024-12-11 21:31:07
 * @LastEditors: fk 1031041088@qq.com
 * @LastEditTime: 2024-12-11 22:51:23
 */

// 密码验证
function verifyPassword() {
    const password = document.getElementById('passwordInput').value;
    if (password === '123456') {
        document.getElementById('passwordLayer').style.display = 'none';
        document.getElementById('uploadContainer').style.display = 'block';
    } else {
        alert('密码错误，请重试！');
    }
}

// 密码提交事件
document.getElementById('submitPassword').addEventListener('click', verifyPassword);
document.getElementById('passwordInput').addEventListener('keypress', (e) => {
    if (e.key === 'Enter') {
        verifyPassword();
    }
});

// 文件上传相关
document.getElementById('fileInput').addEventListener('change', async (e) => {
    const files = e.target.files;
    
    if (files.length === 0) {
        alert('请选择要上传的图片');
        return;
    }
    
    const preview = document.getElementById('preview');
    
    for (let file of files) {
        // 创建预览元素
        const previewItem = document.createElement('div');
        previewItem.className = 'preview-item';
        
        // 创建图片预览
        const img = document.createElement('img');
        const reader = new FileReader();
        reader.onload = (e) => img.src = e.target.result;
        reader.readAsDataURL(file);
        
        // 创建进度条
        const progressDiv = document.createElement('div');
        progressDiv.className = 'upload-progress';
        const progressBar = document.createElement('div');
        progressBar.className = 'progress-bar';
        progressDiv.appendChild(progressBar);
        
        previewItem.appendChild(img);
        previewItem.appendChild(progressDiv);
        preview.appendChild(previewItem);
        
        // 上传文件
        const formData = new FormData();
        formData.append('image', file);
        
        try {
            const response = await fetch('/api/upload.php', {
                method: 'POST',
                body: formData
            });
            
            const result = await response.json();
            if (result.success) {
                progressBar.style.width = '100%';
                progressBar.style.background = '#52c41a';
                
                // 添加上传成功状态
                const status = document.createElement('div');
                status.className = 'upload-status';
                status.textContent = '上传成功';
                previewItem.appendChild(status);
            } else {
                throw new Error(result.error);
            }
        } catch (error) {
            progressBar.style.background = '#ff4d4f';
            
            // 添加上传失败状态
            const status = document.createElement('div');
            status.className = 'upload-status';
            status.textContent = '上传失败';
            previewItem.appendChild(status);
            
            console.error('上传失败：', error);
        }
    }
});

// 拖拽上传支持
const uploadArea = document.getElementById('uploadArea');

uploadArea.addEventListener('dragover', (e) => {
    e.preventDefault();
    uploadArea.style.background = '#f0f9ff';
});

uploadArea.addEventListener('dragleave', (e) => {
    e.preventDefault();
    uploadArea.style.background = '#e6f7ff';
});

uploadArea.addEventListener('drop', (e) => {
    e.preventDefault();
    uploadArea.style.background = '#e6f7ff';
    
    const files = e.dataTransfer.files;
    if (files.length > 0) {
        document.getElementById('fileInput').files = files;
        document.getElementById('fileInput').dispatchEvent(new Event('change'));
    }
}); 